import Vue from "vue";
import VueRouter from "vue-router";
import {WallpaperTypeService} from "../services";
const component = () => import( /* webpackChunkName: 'index.vue' */ "@/views/index.vue")

Vue.use(VueRouter);

const routes = [{
  path: "/",
  name: "all",
  title: "全部",
  icon: "iconfont icon-all",
  props: (route) => ({
    type: route.query.type
  }),
  component
}]

const router = new VueRouter({
  mode: "hash",
  routes,
});


WallpaperTypeService.list().then(response => {
  const { data, success, message } = response;
  if (success) {
    const { rows = [], total } = data;

     const addRoutes = rows.map(row => {
      return {
        _id: row._id,
        path: row.path,
        name: row.name,
        title: row.title,
        icon: row.icon,
        props: (route) => ({
          type: route.query.type
        }),
        component
      }
    });
    router.addRoutes(addRoutes);
    router.options.routes.push(...addRoutes);
  }
})

export default router;
